package com.lpl.service.impl;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import com.lpl.polo.qiandaos;
import com.lpl.polo.result;
import com.lpl.polo.selectDao;
import com.lpl.service.qiandao;
import com.lpl.util.local;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.List;
import java.util.Map;

@Service
public class qiandaompl implements qiandao {
    @Autowired
    private com.lpl.mapper.chadao chadao;
    @Override
    public result insertDao( List<qiandaos> qiandaos) {
        Map<String, Object> A= local.get();
        // 转换List为JSON格式的字符串

        ObjectMapper objectMapper = new ObjectMapper();

//看数据库是否存在
        selectDao u= chadao.selectUserdao((String) A.get("id"));

        System.out.println(u);
//        不存在插入
if(u==null){
    try {
        String json = objectMapper.writeValueAsString(qiandaos);
//        存入数据库
            chadao.daologin((String) A.get("id"),json);
    } catch (JsonProcessingException e) {
        e.printStackTrace();
        return result.error("签到失败");

    }
    return result.success("签到成功");

}
//存在，修改
else {
    try {
        String json = objectMapper.writeValueAsString(qiandaos);
//        修改数据库
        chadao.UpdateDao((String) A.get("id"),json);
    } catch (JsonProcessingException e) {
        e.printStackTrace();
        return result.error("签到失败");
    }
    return result.success("签到成功");


}


    }
}
